import { lineOptions, barOptions, radarOptions, stackedOptions, WaterfallOptions, stackedHorizontalOptions, liquidFillOptions, halfDoughnutOptions, basicAreaOptions, stackedAreOptions, stackedLineOptions} from '@/components/echarts/EchartsOption.js'
export const HandleOptionMixin = {
  computed: {
    lineOptions(){
      let optionsStatistics = this.getStatistics('月利润趋势');
      if(!optionsStatistics?.dataList?.length) return {};
      let optionsStatisticsClone = Object.assign({}, optionsStatistics);
      //排序
      optionsStatisticsClone?.dataList?.sort((a, b) => a?.dateStr?.slice(0, a?.dateStr?.length - 1) - b?.dateStr?.slice(0, b?.dateStr?.length - 1));
      return lineOptions(optionsStatisticsClone);
    },
    barOptions(){
      let sortArr = ['营业收入', '营业成本', '销售费用', '管理费用', '财务费用', '净利润'];
      let optionsStatistics = this.getStatistics('利润表展示');
      if(!optionsStatistics?.dataList?.length) return {};
      let optionsStatisticsClone = Object.assign({}, optionsStatistics);
      //排序
      optionsStatisticsClone?.dataList?.sort((a, b) => sortArr?.indexOf(a?.item) - sortArr?.indexOf(b?.item));
      return barOptions(optionsStatisticsClone);
    },
    radarOptions(){
      let optionsStatistics = this.getStatistics('营运能力展示');
      if(!optionsStatistics?.dataList?.length) return {};
      //营运能力展示下是否有数据
      let isData = optionsStatistics?.dataList?.some(item => item?.dataList?.length);
      if(!isData) return {};
      return radarOptions(optionsStatistics);
    },
    stackedOptions(){
      let optionsStatistics = this.getStatistics('盈利能力展示');
      if(!optionsStatistics?.dataList?.length) return {};
      return stackedOptions(optionsStatistics, this.dateType);
    },
    WaterfallOptions(){
      let optionsStatistics = this.getStatistics('现金净流量增长情况');
      if(!optionsStatistics?.dataList?.length) return {};
      let optionsStatisticsClone = Object.assign({}, optionsStatistics);
      //排序
      optionsStatisticsClone?.dataList?.sort((a, b) => a?.dateStr?.slice(0, a?.dateStr?.length - 1) - b?.dateStr?.slice(0, b?.dateStr?.length - 1));
      return WaterfallOptions(optionsStatisticsClone)
    },
    stackedHorizontalOptions(){
      let sortArr = ['经营活动现金', '投资活动现金', '筹资活动现金', '结余'];
      let optionsStatistics = this.getStatistics('资金收支情况');
      if(!optionsStatistics?.dataList?.length) return {};
      let optionsStatisticsClone = Object.assign({}, optionsStatistics);
      //排序
      optionsStatisticsClone.dataList = optionsStatisticsClone?.dataList?.map(item => {
        let result = {};
        for(let key of sortArr){
          result[key] = item?.dataList?.[key]
        }
        return {
          ...item,
          dataList: result
        }
      })
      return stackedHorizontalOptions(optionsStatisticsClone);
    },
    liquidFillOptions(){
      let sortArr = ['退款', '广告费', '平台手续费', '月租', '其他交易费', 'FBA手续费', '提现'];
      let colorList = ['#1890ff', '#62d9ad', '#5b6e96', '#b7d8ee', '#ffdc4c', '#ff974c', '#e65a56'];
      let optionsStatistics = this.getStatistics('回款情况展示');
      //子页面为数组类型
      if(!optionsStatistics?.dataList?.length) return [];
      let optionsStatisticsClone = Object.assign({}, optionsStatistics);
      //排序
      optionsStatisticsClone?.dataList?.sort((a, b) => sortArr?.indexOf(a?.item) - sortArr?.indexOf(b?.item));
      //返回数组
      return optionsStatisticsClone?.dataList?.map((item, index) => liquidFillOptions(item, colorList?.[index], sortArr?.[index])) || []
    },
    halfDoughnutOptions(){
      let sortArr = ['应收账款周转（天）', '存货周转（天）'];
      let colorList = [['#5aaef3', '#62d9ad'], ['#ffdc4c', '#ff974c']];
      let optionsStatistics = this.getStatistics('营运能力分析');
      //子页面为数组类型
      if(!optionsStatistics?.dataList?.length) return [];
      //排序
      optionsStatistics?.dataList?.sort((a, b) => a?.dateStr?.slice(0, a?.dateStr?.length - 1) - b?.dateStr?.slice(0, b?.dateStr?.length - 1));
      //转换数据类型
      let result = []
      let typeList = optionsStatistics?.dataList?.[0]?.dataList ? Object.keys(optionsStatistics.dataList[0].dataList) : [];
      let timeList = optionsStatistics?.dataList?.map(item => item?.dateStr) || [];
      for(let key of typeList){
        let timeRsult = {};
        timeList?.forEach((item, index) => {
          timeRsult[item] = optionsStatistics?.dataList?.[index]?.dataList?.[key] || '';
        })
        result.push({
          dataList: timeRsult,
          dateStr: key
        })
      }
      let optionsStatisticsClone = {
        ...optionsStatistics,
        dataList: result
      }
      //排序
      optionsStatisticsClone?.dataList?.sort((a, b) => sortArr?.indexOf(a?.dateStr) - sortArr?.indexOf(b?.dateStr));
      //返回数组
      return optionsStatisticsClone?.dataList?.map((item, index) => halfDoughnutOptions(item, colorList?.[index])) || [];
    },
    basicAreaOptions(){
      let sortArr = ['金额', '回款率'];
      let xAxisSortArr = ['店铺收入', '平台放款', '资金流入'];
      let optionsStatistics = this.getStatistics('平台回款情况');
      if(!optionsStatistics?.dataList?.length) return {};
      let optionsStatisticsClone = Object.assign({}, optionsStatistics);
      //排序
      optionsStatisticsClone?.dataList.sort((a, b) => sortArr?.indexOf(a?.name) - sortArr?.indexOf(b?.name));
      //排序
      optionsStatisticsClone.dataList = optionsStatisticsClone?.dataList?.map(item => {
        let result = {};
        for(let key of xAxisSortArr){
          result[key] = item?.dataList?.[key]
        }
        return {
          ...item,
          dataList: result
        }
      }) || []
      return basicAreaOptions(optionsStatisticsClone);
    },
    stackedAreOptions(){
      let sortArr = ['营业收入', '营业成本', '净利润', '净利润率'];
      let optionsStatistics = this.getStatistics('月净利润趋势');
      if(!optionsStatistics?.dataList?.length) return {};
      let optionsStatisticsClone = Object.assign({}, optionsStatistics);
      //排序
      optionsStatisticsClone?.dataList?.sort((a, b) => a?.dateStr?.slice(0, a?.dateStr?.length - 1) - b?.dateStr?.slice(0, b?.dateStr?.length - 1));
      optionsStatisticsClone.dataList = optionsStatisticsClone?.dataList?.map(item => {
        let result = {};
        for(let key of sortArr){
          result[key] = item?.dataList?.[key]
        }
        return {
          ...item,
          dataList: result
        }
      })
      return stackedAreOptions(optionsStatisticsClone);
    },
    stackedLineOptions(){
      let optionsStatistics = this.getStatistics('年度发展趋势');
      if(!optionsStatistics?.dataList?.length) return {};
      let optionsStatisticsClone = Object.assign({}, optionsStatistics);
      //排序
      optionsStatisticsClone?.dataList?.sort((a, b) => a?.name?.slice(0, a?.name?.length-1) - b?.name?.slice(0, b?.name?.length-1));
      //数据为空
      let isDataFlag = optionsStatisticsClone?.dataList?.some(item => {
        return item?.dataList && Object.keys(item.dataList)?.length
      })
      if(!isDataFlag) return {};
      return stackedLineOptions(optionsStatisticsClone);
    },
  },
  methods: {
    getStatistics(title){
      if(!this.statisticsList?.length) return;
      let optionsStatistics = this.statisticsList?.filter(item => {
        return item.name === title;
      })
      optionsStatistics = optionsStatistics?.[0];
      return optionsStatistics;
    }
  }
}